const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getData(data);
				break;
			case 'queryOne':
				res.data = await getDataOne(data);
				break;
			case 'queryTwo':
				res.data = await getDataTwo(data);
				break;
			case 'queryThree':
				res.data = await getDataThree(data);
				break;
			case 'queryById':
				res.data = await getDataById(data);
				break;
			case 'edit':
				res.data = await editData(data);
				break;
			case 'add':
				res.data = await addData(data);
				break;
			case 'addOne':
				res.data = await addDataOne(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}
/**
 * 获取数据
 */
async function getData(data) {
	var result = await db.collection('point').get();
	return result.data;
}
/**
 * 获取用户积分数据
 */
async function getDataOne(data) {
	var result = await db.collection('point_user')
		.where({
			userId: data.userId
		})
		.get();
	return result.data;
}
/**
 * 获取获取积分数据
 */
async function getDataTwo(data) {
	var result = await db.collection('point_list')
		.where({
			userId: data.userId,
			state: 0,
			date: data.date
		})
		.limit(7)
		.orderBy('updatetime', 'desc')
		.get();
	return result.data;
}
/**
 * 获取获取积分数据
 */
async function getDataThree(data) {
	var result = await db.collection('point_list')
		.where({
			state: 0
		})
		.orderBy('updatetime', 'desc')
		.get();
	return result.data;
}
/**
 * 获取数据
 */
async function getDataById(data) {
	var result = await db.collection('point').doc(data.id).get();
	return result.data;
}
/**
 * 编辑数据
 */
async function editData(data) {
	var res = {}
	const result = await db.collection('point').doc(data.id).update({
		data: data.data,
		updatetime: data.updatetime
	})
	res.data = result
	if (result.updated === 1) {
		res.code = 200;
		res.msg = '更新数据成功';
	} else {
		res.code = 402;
		res.msg = '保存失败，请重新操作'
	}
	return res;
}
/**
 * 添加数据
 */
async function addData(data) {
	var res = {}
	const result = await db.collection('point_list').add({
		userId: data.userId,
		date: data.date,
		mode: data.mode, // 0签到获取积分  1消费获取积分
		point: data.point,
		state: data.state, //0表示获取积分 1表示消费积分
		updatetime: data.updatetime
	})
	const res1 = await db.collection('point_user').where({
		userId: data.userId
	}).get()
	if (res1.data.length > 0) {
		await db.collection('point_user').where({
			userId: data.userId
		}).update({
			date: data.date,
			dayNum: data.dayNum,
			point: data.totalPoint,
			updatetime: data.updatetime
		})
	} else {
		//说明是首次签到用户
		await db.collection('point_user').add({
			userId: data.userId,
			date: data.date,
			point: data.point,
			dayNum: 1,
			updatetime: data.updatetime
		})
	}
	res.code = 200;
	res.data = result.data
	return res;
}
/**
 * 添加消费积分数据
 */
async function addDataOne(data) {
	var res = {}
	const result = await db.collection('point_list').add({
		userId: data.userId,
		orderId: data.orderId,
		date: data.date,
		mode: data.mode, // 0签到获取积分  1消费获取积分
		point: data.point, //使用了多少积分
		state: data.state, //0表示获取积分 1表示消费积分
		updatetime: data.updatetime
	})
	await db.collection('point_user').where({
		userId: data.userId
	}).update({
		point: parseInt(data.totalPoint) - parseInt(data.point), //用户剩余积分
		updatetime: data.updatetime
	})

	res.code = 200;
	res.data = result.data
	return res;
}
